targetScope = 'subscription'

metadata name = 'Using large parameter set'
metadata description = 'This instance deploys the module with most of its features enabled.'

// ========== //
// Parameters //
// ========== //

@description('Optional. The name of the resource group to deploy for testing purposes.')
@maxLength(90)
param resourceGroupName string = 'dep-${namePrefix}-network.expressroutecircuits-${serviceShort}-rg'

@description('Optional. The location to deploy resources to.')
param resourceLocation string = deployment().location

@description('Optional. A short identifier for the kind of deployment. Should be kept short to not run into resource-name length-constraints.')
param serviceShort string = 'nercmax'

@description('Optional. A token to inject into the name of each resource.')
param namePrefix string = '#_namePrefix_#'

// ============ //
// Dependencies //
// ============ //

// General resources
// =================
resource resourceGroup 'Microsoft.Resources/resourceGroups@2025-04-01' = {
  name: resourceGroupName
  location: resourceLocation
}

module nestedDependencies 'dependencies.bicep' = {
  scope: resourceGroup
  name: '${uniqueString(deployment().name, resourceLocation)}-nestedDependencies'
  params: {
    managedIdentityName: 'dep-${namePrefix}-msi-${serviceShort}'
    location: resourceLocation
  }
}

// Diagnostics
// ===========
module diagnosticDependencies '../../../../../../../utilities/e2e-template-assets/templates/diagnostic.dependencies.bicep' = {
  scope: resourceGroup
  name: '${uniqueString(deployment().name, resourceLocation)}-diagnosticDependencies'
  params: {
    storageAccountName: 'dep${namePrefix}diasa${serviceShort}01'
    logAnalyticsWorkspaceName: 'dep-${namePrefix}-law-${serviceShort}'
    eventHubNamespaceEventHubName: 'dep-${namePrefix}-evh-${serviceShort}'
    eventHubNamespaceName: 'dep-${namePrefix}-evhns-${serviceShort}'
    location: resourceLocation
  }
}

// ============== //
// Test Execution //
// ============== //
@batchSize(1)
module testDeployment '../../../main.bicep' = [
  for iteration in ['init', 'idem']: {
    scope: resourceGroup
    name: '${uniqueString(deployment().name, resourceLocation)}-test-${serviceShort}-${iteration}'
    params: {
      name: '${namePrefix}${serviceShort}001'
      location: resourceLocation
      bandwidthInMbps: 50
      peeringLocation: 'Amsterdam'
      serviceProviderName: 'Equinix'
      peerings: [
        {
          name: 'AzurePrivatePeering'
          properties: {
            peeringType: 'AzurePrivatePeering'
            peerASN: 65001
            primaryPeerAddressPrefix: '10.0.0.0/30'
            secondaryPeerAddressPrefix: '10.0.0.4/30'
            vlanId: 100
            state: 'Enabled'
            ipv6PeeringConfig: {
              primaryPeerAddressPrefix: '2001:db8::/126'
              secondaryPeerAddressPrefix: '2001:db8::8/126'
            }
          }
        }
        {
          name: 'MicrosoftPeering'
          properties: {
            peeringType: 'MicrosoftPeering'
            peerASN: 65002
            primaryPeerAddressPrefix: '203.0.113.0/30'
            secondaryPeerAddressPrefix: '203.0.113.4/30'
            vlanId: 200
            state: 'Disabled'
            microsoftPeeringConfig: {
              advertisedPublicPrefixes: [
                '203.0.113.0/24'
              ]
              customerASN: 65002
              routingRegistryName: 'ARIN'
            }
            ipv6PeeringConfig: {
              primaryPeerAddressPrefix: '2001:db8:100::/126'
              secondaryPeerAddressPrefix: '2001:db8:100::8/126'
              microsoftPeeringConfig: {
                advertisedPublicPrefixes: [
                  '2001:db8:200::/48'
                ]
                customerASN: 65002
                routingRegistryName: 'ARIN'
              }
            }
          }
        }
      ]
      globalReachEnabled: true
      diagnosticSettings: [
        {
          name: 'customSetting'
          metricCategories: [
            {
              category: 'AllMetrics'
            }
          ]
          eventHubName: diagnosticDependencies.outputs.eventHubNamespaceEventHubName
          eventHubAuthorizationRuleResourceId: diagnosticDependencies.outputs.eventHubAuthorizationRuleId
          storageAccountResourceId: diagnosticDependencies.outputs.storageAccountResourceId
          workspaceResourceId: diagnosticDependencies.outputs.logAnalyticsWorkspaceResourceId
        }
      ]
      enableDirectPortRateLimit: true
      lock: {
        kind: 'CanNotDelete'
        name: 'myCustomLockName'
      }
      roleAssignments: [
        {
          name: 'd7aa3dfa-6ba6-4ed8-b561-2164fbb1327e'
          roleDefinitionIdOrName: 'Owner'
          principalId: nestedDependencies.outputs.managedIdentityPrincipalId
          principalType: 'ServicePrincipal'
        }
        {
          name: guid('Custom seed ${namePrefix}${serviceShort}')
          roleDefinitionIdOrName: 'b24988ac-6180-42a0-ab88-20f7382dd24c'
          principalId: nestedDependencies.outputs.managedIdentityPrincipalId
          principalType: 'ServicePrincipal'
        }
        {
          roleDefinitionIdOrName: subscriptionResourceId(
            'Microsoft.Authorization/roleDefinitions',
            'acdd72a7-3385-48ef-bd42-f606fba81ae7'
          )
          principalId: nestedDependencies.outputs.managedIdentityPrincipalId
          principalType: 'ServicePrincipal'
        }
      ]
      skuFamily: 'MeteredData'
      skuTier: 'Standard'
      allowClassicOperations: true
      tags: {
        'hidden-title': 'This is visible in the resource name'
        Environment: 'Non-Prod'
        Role: 'DeploymentValidation'
      }
    }
  }
]
